/**
 * Copyright 2012 Simple-Jdbc-Lightweight

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package org.mlu.jdbc.simple;

import java.io.FileNotFoundException;
import java.io.InputStream;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * Simple jdbc implementation base class
 * 
 * Generally you would extend this class 
 *
 * @author Mlungisi Sincuba
 * @since 1.0
 */
public class BaseSimpleJdbcImpl {
    protected static final Logger logger = LoggerFactory.getLogger(BaseSimpleJdbcImpl.class);
    private SimpleJdbc jdbc;
    private DataSource dataSource;
    
    /**
     * Uses db.properties from project path or class path, starting at project path;
     * 
     */
    public BaseSimpleJdbcImpl() {
        if (dataSource == null) {
            try {
                dataSource = DataSourceDetails.getDataSource();
            } catch (FileNotFoundException ex) {
                if (dataSource == null) {
                    dataSource = DataSourceDetails.getDataSourceFromClassPath();
                }
                logger.debug("File 'db.properties' was not found");
            }
            if (dataSource == null) {
                dataSource = DataSourceDetails.getDataSourceFromClassPath();
            }
        }
        logger.debug("Using datasource: " + dataSource);
        if (jdbc == null) {
            jdbc = new SimpleJdbc(getDataSource());
        }
    }
    
    /**
     * 
     * Used main on stand-alone applications
     * 
     * @param fileName properties file on project path
     */
    public BaseSimpleJdbcImpl(String fileName) {
        if (fileName == null) {
            fileName = "db.properties";
        }
        if (dataSource == null) {
            try{
                dataSource = DataSourceDetails.getDataSource(fileName);
            } catch(FileNotFoundException ex) {
                if (dataSource == null) {
                    fileName = fileName.substring(0, fileName.indexOf("."));
                    dataSource = DataSourceDetails.getDataSourceFromClassPath(fileName);
                }
                logger.debug("File " + fileName + " was not found");
            }
            
        }
        logger.debug("Using datasource: " + dataSource);
        if (jdbc == null) {
            jdbc = new SimpleJdbc(getDataSource());
        }
    }

    /**
     * 
     * @param input which contains properties file from either class path or WEB-INF path
     * 
     * Usage example:
     * InputStream input = getServletContext().getResourceAsStream("/WEB-INF/db.properties");
     * BaseSimpleJdbcSubclass x = new BaseSimpleJdbcSubclassImpl(input);
     * 
     * 
     */
    public BaseSimpleJdbcImpl(InputStream input) {
        if (dataSource == null) {
            dataSource = DataSourceDetails.getDataSourceForWeb(input);
        }
        logger.debug("Using datasource: " + dataSource);
        if (jdbc == null) {
            jdbc = new SimpleJdbc(getDataSource());
        }
    }

    /**
     * @return the jdbc
     */
    public SimpleJdbc getJdbc() {
        return jdbc;
    }

    /**
     * @param jdbc the jdbc to set
     */
    public void setJdbc(SimpleJdbc jdbc) {
        this.jdbc = jdbc;
    }

    /**
     * @return the dataSource
     */
    public final DataSource getDataSource() {
        return dataSource;
    }

    /**
     * @param dataSource the dataSource to set
     */
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
}
